package org.classg.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.classg.edu.entity.Teacher;
import org.classg.edu.service.TeacherService;
import org.classg.edu.vo.TeacherQuery;
import org.classg.servicebase.exception.MyException;
import org.classg.utils.ExceptionUtil;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-24
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有讲师
     * @return
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public R findAll() {
        return R.ok().data("teachers", teacherService.list(null));
    }


    /**
     * 根据id删除讲师
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/removeById/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 讲师列表分页
     */
    @ApiOperation(value = "讲师列表分页功能")
    @PostMapping("findByPage/{current}/{limit}")
    public R findByPage(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页显示的数量", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 多条件组合查询，使用动态sql来拼接
        System.out.println(teacherQuery);
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        IPage<Teacher> page = new Page<>(current, limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
        // 判断添加是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }

        // 根据时间排序
        queryWrapper.orderByDesc("gmt_create");

        IPage<Teacher> iPage = teacherService.page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("Total", iPage.getTotal());// 总记录数
        map.put("rows", iPage.getRecords());// 数据集合
        return R.ok().data(map);
    }

    /**
     * 新增讲师
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("saveTeacher")
    public R saveTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return R.ok();
    }

    /**
     * 根据讲师id查询讲师
     */
    @ApiOperation(value = "根据讲师id查询讲师")
    @GetMapping("findByTeacherId/{id}")
    public R findByTeacherId(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        try {

        } catch (Exception e) {
            log.error(ExceptionUtil.getMessage(e));
            throw new MyException(20001, "出现自定义异常了");
        }
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    /**
     * 根据id修改讲师
     */
    @ApiOperation(value = "根据id修改讲师")
    @PutMapping("updateByTeacherId/{id}")
    public R updateByTeacherId(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

}

